<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\Project\Project as ProjectModel;
use app\common\validate\Project\Project as ProjectValidate;
use think\exception\ValidateException;
use think\facade\Validate;

class Project extends BaseController
{
    protected $middleware = [
        \app\admin\middleware\LoginAuth::class
    ];
    public function index()
    {
        $menu = [
            'name' => '预约管理',
            'title' => '预约项目'
        ];
        $searchfield = $this->request->param('search_field', 'name');
        $keyword = $this->request->param('keyword');
        $where = [];
        if ($searchfield && $keyword) {
            $where[] = [$searchfield, 'like', "%$keyword%"];
        }
        $num = 8;
        $ProjectList = ProjectModel::where($where)->paginate(['list_rows' => $num, 'query' => request()->param()]);
        // 当前页
        $page = $ProjectList->currentPage();
        return view('', [
            'menu' => $menu,
            'ProjectList' => $ProjectList,
            'num' => $num,
            'page' => $page
        ]);
    }
    // 新增
    public function add()
    {
        $menu = [
            'name' => '预约管理',
            'title' => '新增服务'
        ];
        if ($this->request->isPost()) {
            $name = $this->request->param('name');
            $data = [
                'name' => $name
            ];
            try {
                Validate(ProjectValidate::class)->check($data);
            } catch (ValidateException $e) {
                $this->error($e->getMessage());
            }
            $result = ProjectModel::create($data);
            if ($result === FALSE) {
                $this->error('新增失败');
            }
            $this->success('新增成功', (string)url('/admin/project/index'));
        }
        return view('', [
            'menu' => $menu,
        ]);
    }
    // 编辑
    public function edit(){
        $menu = [
            'name' => '预约管理',
            'title' => '编辑服务'
        ];
        $projectid=$this->request->param('projectid');
        $Project=ProjectModel::find($projectid);
        if(!$Project){
            $this->error('该预约服务不存在',(String)url('/admin/project/index'));
        }
        if ($this->request->isPost()) {
            $name = $this->request->param('name');
            $data = [
                'name' => $name
            ];
            try {
                Validate(ProjectValidate::class)->check($data);
            } catch (ValidateException $e) {
                $this->error($e->getMessage());
            }
            $result = ProjectModel::update($data,['id'=>$Project['id']]);
            if ($result === FALSE) {
                $this->error('修改失败');
            }
            $this->success('修改成功', (string)url('/admin/project/index'));
        }
        return view('', [
            'menu' => $menu,
            'Project'=>$Project
        ]);
    }
    // 异步查询是否重名
    public function select()
    {
        if ($this->request->isAjax()) {
            $name = $this->request->param('name');
            $Project = ProjectModel::where(['name' => $name])->find();
            if ($Project) {
                $this->error('该售后服务名称已存在，请重新填写');
            }
        }
    }
    // 删除
    public function del(){
        $projectid=$this->request->param('projectid');
        $ids=$this->request->param('ids');
        $action=$this->request->param('action');
        if($action=='del'){
            $Project=ProjectModel::find($projectid);
            if (!$Project) {
                $this->error('此预约项目不存在！');
            }
            $result=ProjectModel::destroy($projectid);
            if (!$result) {
                $this->error('删除失败！');
            }
            $this->success('删除成功！');
        }
        if ($action=='dels') {
            $ProjectList=ProjectModel::whereIn('id',$ids)->select();
            foreach ($ProjectList as $key => $item) {
                if(!in_array($item['id'],$ids)){
                    $this->error("所选项目{$item['name']}不存在,请重新选择");
                }
            }
            $result=ProjectModel::destroy($ids);
            if (!$result) {
                $this->error('批量删除失败！');
            }
            $this->success('批量删除成功！');
        }
    }
}
